package com.chen.blue.domain.model.system;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Collection;
import java.util.Objects;

/**
 * Created By CLT On 2023/10/30.
 */
@SuppressWarnings("all")
@Getter
@Setter
@ToString(callSuper = false)
@ApiModel("登录用户")
public class LoginUser implements UserDetails, Serializable {

    private static final long serialVersionUid = 1L;


    @NotBlank(message = "用户名不为空")
    @ApiModelProperty(value = "用户名")
    private String username;

    @NotBlank(message = "密码不为空")
    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "token")
    private String token;

    @ApiModelProperty(value = "验证码")
    private String code;

    @JsonIgnore
    @ApiModelProperty(value = "系统用户信息")
    private UsersModel sysUser;

    public String getUsername() {
        if (Objects.nonNull(sysUser)) {
            return sysUser.getUsername();
        }
        return this.username;
    }

    public String getPassword() {
        if (Objects.nonNull(sysUser)) {
            return sysUser.getPassword();
        }
        return this.password;
    }

    /**
     * 用户角色
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    /**
     * 账户是否过期，过期无法验证
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否锁定，锁定无法验证
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 凭证是否过期，过期无法验证
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 用户是否可用，不可用无法验证
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
